<?php

/*
 * All functions related to image processing
 * 
 */

/**
 * Description of ImageBehavior
 *
 * @author sadaf.siddiqui
 */
class ImageBehavior extends CActiveRecordBehavior {

//    public $mediaFile;

    const FOLDER = "media";

    public function imagePath() {
        return Yii::app()->basePath . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . self::FOLDER . DIRECTORY_SEPARATOR . $this->owner->media;
    }

    public function imageWebPath() {
        return Yii::app()->baseUrl . DIRECTORY_SEPARATOR . self::FOLDER . DIRECTORY_SEPARATOR . $this->owner->media;
    }

    public function beforeSave($event) {
        if ($this->owner->mediaFile)
            $this->owner->media = substr(str_replace (" ", "", $this->owner->name), rand(1, strlen($this->owner->name) - 1), 7) . '_' . rand(1,100).".".$this->owner->mediaFile->extensionName;

        parent::beforeSave($event);
        return true;
    }

//    private function setImage(){
//        
//    }
}

?>
